﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace BaseTypesControlLib.Controls
{
    public class NumericChoiceControl:ChoiceFieldControl
    {
        static NumericChoiceControl()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(0,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            StartProperty = DependencyProperty.Register("Start", typeof(int),
                typeof(NumericChoiceControl), fpm);
            fpm = new FrameworkPropertyMetadata(0,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            EndProperty = DependencyProperty.Register("End", typeof(int),
                typeof(NumericChoiceControl), fpm);
            fpm = new FrameworkPropertyMetadata(1,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure, OnPropertyChanged);
            IncrementProperty = DependencyProperty.Register("Increment", typeof(int),
                typeof(NumericChoiceControl), fpm);
        }
        

        public int Start
        {
            get { return (int)GetValue(StartProperty); }
            set { SetValue(StartProperty, value); }
        }
        public static readonly DependencyProperty StartProperty ;

        public int End
        {
            get { return (int)GetValue(EndProperty); }
            set { SetValue(EndProperty, value); }
        }
        public static readonly DependencyProperty EndProperty;

        public int Increment
        {
            get { return (int)GetValue(IncrementProperty); }
            set { SetValue(IncrementProperty, value); }
        }
        public static readonly DependencyProperty IncrementProperty;



        private static void OnPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            NumericChoiceControl ctrl = o as NumericChoiceControl;
            if (ctrl != null)
                ctrl.UpdateText();
        }

        protected override void UpdateText()
        {
            if (Start < End && Increment > 0)
            {
                List<int> items = new List<int>();
                for (int i = Start; i < End; i += Increment)
                    items.Add(i);
                if (items.Last() != End) items.Add(End);

                if (SelectedIndex >= 0 && SelectedIndex < items.Count)
                {
                    txtText.Text = items[SelectedIndex].ToString();
                    return;
                }
            }
            txtText.Text = "";
        }
    }
}
